package com.zmn.oms.zmn.business.impl.work.orderpay;

import com.alibaba.fastjson.JSON;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.oms.common.dro.orderpay.OrderPayResultDRO;
import com.zmn.oms.common.dto.OmsPayRequestDTO;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.zmn.business.interfaces.work.orderpay.OrderPayResultBService;
import com.zmn.pay.common.constant.PayConsts;
import com.zmn.pay.gateway.common.model.dro.PayResultDRO;
import com.zmn.pay.gateway.common.model.dro.PayStatusDRO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 类描述：订单支付结果处理类
 * @author heciqi
 * @date 2019/01/08 20:14
 */
@Slf4j
@Service
public class OrderPayResultBServiceImpl implements OrderPayResultBService {
	@Autowired
	OrderWorkService orderWorkService;
	/**
	 * 转化成订单支付模型
	 * @param payResultDRO
	 * @return
	 */
	public OrderPayResultDRO convertOrderPayResult(PayResultDRO payResultDRO) {
		// 1，转化成订单支付模型
		PayStatusDRO payDRO = payResultDRO.getResult();
		OrderPayResultDRO orderPayResultDRO = this.convertOrderPayResult(payDRO);

		// 2，处理合并支付
		if (Objects.equals(payResultDRO.getMergeFlag(), GlobalConsts.YES)) {
			PayStatusDRO mergeResult = payResultDRO.getMergeResult();
			orderPayResultDRO.setMergeInnerTradeNo(mergeResult.getInnerTradeNo());
			orderPayResultDRO.setMergePayOutTradeNo(mergeResult.getOutTradeNo());
			orderPayResultDRO.setMergeAmount(mergeResult.getAmount());
			orderPayResultDRO.setInnerTradeNo(mergeResult.getInnerTradeNo());
			orderPayResultDRO.setPayOutTradeNo(mergeResult.getOutTradeNo());
		} else {
			orderPayResultDRO.setInnerTradeNo(payDRO.getInnerTradeNo());
			orderPayResultDRO.setPayOutTradeNo(payDRO.getOutTradeNo());
			orderPayResultDRO.setDiscountGoods(payDRO.getDiscountGoods());
		}
		return orderPayResultDRO;
	}

	/**
	 * 转化成订单支付模型
	 * @param payDRO
	 * @return
	 */
	public OrderPayResultDRO convertOrderPayResult(PayStatusDRO payDRO) {

		// 支付数据
		OrderPayResultDRO orderPayResultDRO = new OrderPayResultDRO();
		orderPayResultDRO.setPayChannelId(payDRO.getChannelId());
		orderPayResultDRO.setPayType(payDRO.getPayType());
		orderPayResultDRO.setPayTargetChannelId(payDRO.getPayTargetChannelId());
		orderPayResultDRO.setPayTargetType(payDRO.getPayTargetType());
		orderPayResultDRO.setPayStatus(payDRO.getPayStatus());
		orderPayResultDRO.setAmount(payDRO.getAmount());
		orderPayResultDRO.setPayTime(payDRO.getPayTime());
		orderPayResultDRO.setAppBizSubNo(payDRO.getAppBizSubNo());

		// 2，业务数据
		OmsPayRequestDTO omsPayRequestDTO = JSON.parseObject(payDRO.getRequestData(), OmsPayRequestDTO.class);
		orderPayResultDRO.setOrderId(omsPayRequestDTO.getOrderId());
		orderPayResultDRO.setWorkId(omsPayRequestDTO.getWorkId());
		orderPayResultDRO.setAmountType(omsPayRequestDTO.getAmountType());
		orderPayResultDRO.setAgentType(omsPayRequestDTO.getAgentType());
		orderPayResultDRO.setTradeType(PayConsts.PAY_TRADE_TYPE_INTIME);
		orderPayResultDRO.setImageUrls(omsPayRequestDTO.getImageUrls());
		if (NumberUtil.isNotNullOrZero(omsPayRequestDTO.getPayerType())) {
			orderPayResultDRO.setPayerType(omsPayRequestDTO.getPayerType());
			orderPayResultDRO.setPayerId(omsPayRequestDTO.getPayerId());
		} else {
			orderPayResultDRO.setPayerType(omsPayRequestDTO.getOperatorType());
			orderPayResultDRO.setPayerId(omsPayRequestDTO.getOperatorId());
		}
		Integer payPlat = NumberUtil.isNotNullOrZero(payDRO.getPayPlat()) ? payDRO.getPayPlat() : payDRO.getPlat();
		if (NumberUtil.isNullOrZero(payPlat)) {
			OrderWork dbOrderWork = orderWorkService.findOrderWorkByKey(orderPayResultDRO.getOrderId(), orderPayResultDRO.getWorkId());
			payPlat = dbOrderWork.getPlatWork();
		}
		orderPayResultDRO.setPayPlat(payPlat);
		orderPayResultDRO.setPayerThirdAccount(StringUtils.isNotBlank(payDRO.getPayAccount()) ? payDRO.getPayAccount() : omsPayRequestDTO.getPayerThirdAccount());
		orderPayResultDRO.setPayerThirdAccountBank(omsPayRequestDTO.getPayerThirdAccountBank());
		orderPayResultDRO.setPayerThirdAccountName(omsPayRequestDTO.getPayerThirdAccountName());
		orderPayResultDRO.setPayeeThirdAccount(omsPayRequestDTO.getPayeeThirdAccount());
		orderPayResultDRO.setPayeeThirdAccountBank(omsPayRequestDTO.getPayeeThirdAccountBank());
		orderPayResultDRO.setPayeeThirdAccountName(omsPayRequestDTO.getPayeeThirdAccountName());
		orderPayResultDRO.setPayerAccountId(0L);// 付款资金账号id，暂未使用，预留
		orderPayResultDRO.setLongitude(omsPayRequestDTO.getLongitude());
		orderPayResultDRO.setLatitude(omsPayRequestDTO.getLatitude());
		orderPayResultDRO.setOperator(omsPayRequestDTO.getOperator());
		orderPayResultDRO.setOperatorType(omsPayRequestDTO.getOperatorType());
		orderPayResultDRO.setOperatorId(omsPayRequestDTO.getOperatorId());
		orderPayResultDRO.setAcceptanceId(omsPayRequestDTO.getAcceptanceId());
		orderPayResultDRO.setOrderPayId(omsPayRequestDTO.getOrderPayId());
		return orderPayResultDRO;
	}
}
